var config = require("config.js");

/** GET方法 */
function httpGet(opt) {
    if (opt.url.indexOf("http://") == -1 && opt.url.indexOf("https://") == -1)
        opt.url = config.API_HOST + opt.url
    wx.request({
        url: opt.url,
        method: 'GET',
        header: opt.header ? opt.header : { "Content-Type": "application/json" },
        success: function (res) {
            requestSuccess(res, opt.fn, opt.handleErrSelf, opt.failFn)
        },
        fail: function (err, a) {
            opt.failFn ? requestFail(err, opt.failFn) : requestFail(err)
        },
        complete: function (res) {
            opt.comFn ? opt.comFn(res) : ''
        }
    });
}

/** PUT方法 */
function httpPut(opt) {
    if (opt.url.indexOf("http://") == -1 && opt.url.indexOf("https://") == -1)
        opt.url = config.API_HOST + opt.url
    wx.request({
        url: opt.url,
        method: 'PUT',
        header: opt.header ? opt.header : { "Content-Type": "application/json" },
        success: function (res) {
            requestSuccess(res, opt.fn, opt.handleErrSelf, opt.failFn)
        },
        fail: function (err, a) {
            opt.failFn ? requestFail(err, opt.failFn) : requestFail(err)
        },
        complete: function (res) {
            opt.comFn ? opt.comFn(res) : ''
        }
    });
}

/** DELETE方法 */
function httpDelete(opt) {
    if (opt.url.indexOf("http://") == -1 && opt.url.indexOf("https://") == -1)
        opt.url = config.API_HOST + opt.url
    wx.request({
        url: opt.url,
        method: 'DELETE',
        header: opt.header ? opt.header : { "Content-Type": "application/json" },
        success: function (res) {
            requestSuccess(res, opt.fn, opt.handleErrSelf, opt.failFn)
        },
        fail: function (err, a) {
            opt.failFn ? requestFail(err, opt.failFn) : requestFail(err)
        },
        complete: function (res) {
            opt.comFn ? opt.comFn(res) : ''
        }
    });
}

// 接口调用成功
function requestSuccess(res, fn, handleErrSelf, failFn) {
    if (200 == res.statusCode) {
        if (handleErrSelf) {
            fn(res)
            return
        }

        if (0 == res.data.code) {
            fn(res)
            //授权失效重新登录
        } else if (10006 == res.data.code) {
            refreshToken().then((res) => {
                if (res) {
                    fn('refreshToken')
                } else {
                    wx.redirectTo({
                        url: '/pages/index/index',
                    })
                }
            })
            return
        } else if (failFn) {
            failFn(res)
        }

    } else {
        wx.showToast({
            title: '当前网络状态较差...',
        })
        console.log('当前网络状态较差...', res)
    }
}

// 接口调用失败
function requestFail(err, failFn) {
    wx.showToast({
        title: '当前网络状态较差...',
        icon: 'loading',
        duration: 1500,
        mask: true,
    })

    failFn ? failFn(err) : console.log('wx接口调用失败，无失败回调函数', err)
}

/** POST方法 */
function httpPost(opt) {
    if (opt.url.indexOf("http://") == -1 && opt.url.indexOf("https://") == -1)
        opt.url = config.API_HOST + opt.url
    wx.request({
        url: opt.url,
        method: 'POST',
        data: opt.body,
        header: opt.header ? opt.header : { "Content-Type": "application/json" },
        success: function (res) {
            requestSuccess(res, opt.fn, opt.handleErrSelf, opt.failFn)
        },
        fail: function (err, a) {
            opt.failFn ? requestFail(err, opt.failFn) : requestFail(err)
        },
        complete: function (res) {
            opt.comFn ? opt.comFn(res) : ''
        }
    });
}

function refreshToken() {
    return new Promise(resolve => {
        const MPLogin = require('./MPLogin.js')
        MPLogin.login(res => {
            res ? resolve(true) : resolve(false)
        })
    })
}


/**显示正在加载，可传入显示消息 */
function showLoading(msg) {
    if (wx.showLoading) {
        wx.showLoading({
            title: msg ? msg : '加载中...',
            mask: true,
        })
    }
}

/**隐藏加载动画 */
function hideLoading() {
    if (wx.hideLoading) {
        wx.hideLoading()
    }
}

module.exports = {
    httpGet: httpGet,
    httpPost: httpPost,
    showLoading: showLoading,
    hideLoading: hideLoading,
}
